package com.cslg.ac.demo.dao.post;

import com.cslg.ac.demo.dao.post.po.PostPO;
import com.cslg.ac.demo.entity.post.Post;
import com.cslg.ac.demo.util.integer.IntegerUtil;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.ws.rs.PathParam;
import java.util.List;
import java.util.Map;

@Mapper
public interface PostDAO {

    String POST_TABLE_NAME = "e_post";
    String USER_POST_TABLE_NAME = "r_user_post";
    String POST_SELECT_FIELD = "id,context,type,comment_count commentCount,target_id targetId,album_count albumCount,deleted, create_time createTime";

    @Insert({"insert into ",POST_TABLE_NAME,"(context, type, target_id, update_time) values(#{context}, #{type}, #{targetId}, now())"})
    void insert(PostPO postPO);

    @Update({"update ",POST_TABLE_NAME,"set deleted = #{deleted}, comment_count = #{commentCount},album_count = #{albumCount}, update_time = now() where id = #{id}"})
    void updateById(PostPO postPO);

    @Insert({"insert into ",USER_POST_TABLE_NAME,"(user_id, post_type) values(#{userId}, #{postType})"})
    void insertUserPost(@Param("userId")int userId, @Param("postType")int postType);

    @Update({"update ",POST_TABLE_NAME,"set deleted = '1' where id = #{id}"})
    void delete(@Param("id")int id);

    @Delete({"delete from ",USER_POST_TABLE_NAME,"where post_id = #{postId}"})
    void deleteUserPost(@Param("postId")int postId);

    @Select({"select ", POST_SELECT_FIELD, "from ", POST_TABLE_NAME, "where id = #{id} and deleted = '0'"})
    PostPO selectById(@Param("id")int id);

    @Select({"select post_id from ", USER_POST_TABLE_NAME, " where user_id = #{userId} and post_type = #{postType} limit #{offset}, #{num}"})
    Integer[] selectUserPostByUser(@Param("userId")int userId, @Param("postType")int postType, @Param("offset")int offset, @Param("num")int num);

    @Select({"select ", POST_SELECT_FIELD," from ",POST_TABLE_NAME, " where id = #{id} and type = #{type}"})
    Integer selectPostByIdAndType(@Param("id")int id, @Param("type")int type);

    @Select({"select id from ", POST_TABLE_NAME, "where target_id = #{targetId} and deleted = '0' limit #{offset}, #{num}"})
    Integer[] selectPostByPost(@Param("targetId")int targetId, @Param("offset")int offset, @Param("num")int num);

    @Select({"select user_id userId from ", USER_POST_TABLE_NAME, "where post_id = #{postId}"})
    Integer selectUserId(@Param("postId")int postId);

    @Select({"select last_insert_id()"})
    Integer selectLastId();

    @Select({"select id from ", POST_TABLE_NAME, "where type = #{type} and target_id = '0' limit #{offset}, #{num}"})
    Integer[] selectPostByType(@Param("type")int type, @Param("offset")int offset, @Param("num")int num);

    @Select({"select id from ", POST_TABLE_NAME, "where type = #{type} and target_id != '0' limit #{offset}, #{num}"})
    Integer[] selectCommentByType(@Param("type")int type, @Param("offset")int offset, @Param("num")int num);

    @Select({"select user_id from ", USER_POST_TABLE_NAME, "where post_id = #{postId}"})
    Integer selectUserByPost(@Param("postId")int postId);


}
